﻿
using GCC.Container.Abstracts;

using GCCGrpc.IMServer;


namespace System.IMServer
{
    internal class WSManager
    {
        public static readonly WSManager Instance = new();
        private WSManager() { }

        private AbsGCCContext? _context;

        public void Init(AbsGCCContext context)
        {
            _context = context;

        }


        public GetAllUserResponse GetAllUser(GetAllUserRequest _)
        {
            var res = new GetAllUserResponse();
            if (_context is null)
            {
                return res;
            }
            foreach (var uid in _context.GetAllUserID())
            {
                var clients = _context.GetUserWSClient<string>(uid);
                clients.ForEach(item =>
                {
                    res.Users.Add(new GetAllUserResponseItem()
                    {
                        UID = uid,
                        ClientID = item.ID,
                        ClientType = item.Type,
                    });
                });

            }

            return res;
        }

        public ForcedOfflineResponse ForcedOffline(ForcedOfflineRequest request)
        {
            var res = new ForcedOfflineResponse();
            if (_context is null)
            {
                return res;
            }
            _context.ForcedOffline<string>(request.UID, String.IsNullOrWhiteSpace(request.ClientID) ? null : request.ClientID);

            return res;
        }

        public SendMessageToAllUserResponse SendMessageToAllUser(SendMessageToAllUserRequest request)
        {
            var res = new SendMessageToAllUserResponse();
            if (_context is null)
            {
                return res;
            }
            _context.BoardCastTextMessage(request.Text);

            return res;
        }

        public SendMessageToUserResponse SendMessageToUser(SendMessageToUserRequest request)
        {
            var res = new SendMessageToUserResponse();
            if (_context is null)
            {
                return res;
            }
            _context.SendTextMessageToUser(request.UID, request.Text);

            return res;
        }

        public SendMessageToUserClientResponse SendMessageToUserClient(SendMessageToUserClientRequest request)
        {
            var res = new SendMessageToUserClientResponse();
            if (_context is null)
            {
                return res;
            }
            _context.SendTextMessageToUserClient(request.UID, request.ClientID, request.Text);

            return res;
        }
    }
}
